<?php
require_once("config.inc.php");
// require_once("function_class.php");
/**
 * 回头客接口
 * @category betterlie
 * @package util.email
 * @subpackage phpmailer
 */
class UtilApi extends Util
{

	static function GET_Api($url, $PostData) {
	    $o = "";
	    foreach ($PostData as $k => $v) {
	        $o .= "$k=" . urlencode($v) . "&";
	    }
	    $post_data = substr($o, 0, - 1);

	    return file_get_contents($url . $post_data);
	}

	static function POST_Api($url, $PostData) {
	    $o = "";
	    foreach ($PostData as $k => $v) {
	        $o .= "$k=" . urlencode($v) . "&";
	    }
	    $post_data = substr($o, 0, - 1);

	    //echo $url.$post_data;die();
	    //请求参数为get方式
	    $curl = curl_init();
	    // 设置你需要抓取的URL
	    curl_setopt($curl, CURLOPT_URL, $url);
	    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
	    // 设置cURL 参数，要求结果保存到字符串中还是输出到屏幕上。
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

	    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
	    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

	    // 运行cURL，请求网页
	    $rdata = curl_exec($curl);

	    // 关闭URL请求
	    curl_close($curl);
	    return $rdata;
	}

	/*
	 * rc4加密算法
	 * $pwd 密钥
	 * $data 要加密的数据
	 */

	static function RC4($pt, $key = H_PASS) {
	    $s = array();
	    for ($i = 0; $i < 256; $i++) {
	        $s[$i] = $i;
	    }
	    $j = 0;
	    $x;
	    for ($i = 0; $i < 256; $i++) {
	        $j = ($j + $s[$i] + ord($key[$i % strlen($key)])) % 256;
	        $x = $s[$i];
	        $s[$i] = $s[$j];
	        $s[$j] = $x;
	    }
	    $i = 0;
	    $j = 0;
	    $ct = '';
	    $y;
	    for ($y = 0; $y < strlen($pt); $y++) {
	        $i = ($i + 1) % 256;
	        $j = ($j + $s[$i]) % 256;
	        $x = $s[$i];
	        $s[$i] = $s[$j];
	        $s[$j] = $x;
	        $ct .= $pt[$y] ^ chr($s[($s[$i] + $s[$j]) % 256]);
	    }

	    $length = strlen($ct);
	    $cipher = '';
	    for ($i = 0; $i < $length; $i++) {
	        $cipher .= sprintf('%02s', dechex(ord($ct[$i])));
	    }

	    $ct = $cipher;

	    return $ct;
	}
	//新增会员
	public static function member_add($data){
		$realname			 = trim($data['realname']);
		$password			 = trim($data['password']);
		$mobile				 = trim($data['mobile']);
		$email				 = trim($data['email']);
		$birthday			 = trim($data['birthday']);
		$gender				 = trim($data['gender']);

		//RC4密码
		$password = self::RC4($password,ApiKey);
		$time = time();
		$PostData = array();
		$PostData['sbs_id']			 = sbs_id;
		$PostData['realname']		 = $realname;
		$PostData['password']		 = $password;
		$PostData['mobile']			 = $mobile;
		$PostData['birthday']		 = $birthday;
		$PostData['gender']			 = $gender;
		$PostData['t']				 = $time;
		$PostData['sign']			 = md5(sbs_id.$realname.$password.$mobile.$birthday.$gender.$time.ApiKey);
		$result = self::POST_Api(ApiURL."member_add.php?",$PostData);
		return json_decode($result);
	}

	//登录
	public static function login($cardno,$passwd){
		//RC4密码
		$password = self::RC4($passwd,DefaultKey);
		//$password = RC4(TestCardPwd,ApiKey);
		$time = time();

		$PostData = array();
		$PostData['sbs_id']		 = sbs_id;
		$PostData['card_no']	 = $cardno;
		$PostData['password']	 = $password;
		$PostData['t']			 = $time;
		$PostData['sign']		 = md5(sbs_id.$cardno.$password.$time.ApiKey);

		$result = self::POST_Api(ApiURL."app_login.php",$PostData);
		return json_decode($result);
	}
	//获取会员信息
	public static function getMember($cardno,$passwd=""){
		//RC4密码
		$cardno = trim($cardno);
		$passwd = trim($passwd);

		$password = self::RC4($passwd,ApiKey);
		$time = time();
		$PostData = array();

		$PostData['sbs_id']		 = sbs_id;
		$PostData['card_no']	 = $cardno;
		$PostData['password']	 = $password;
		$PostData['t']			 = $time;
		$PostData['sign']		 = md5(sbs_id.$cardno.$password.$time.ApiKey);

		$result = self::GET_Api(ApiURL."get_member.php?",$PostData);
		return json_decode($result);
	}

    //修改会员信息
	public static function modify_member($data){
		$cardno		 = trim($data['cardno']);
		$passwd		 = trim($data['passwd']);
		$realname	 = trim($data['realname']);
		$gender		 = trim($data['gender']);
		$birthday	 = trim($data['birthday']);
		$idnum		 = trim($data['idnum']);
		$phone		 = trim($data['phone']);
		$mobile		 = trim($data['mobile']);
		$email		 = trim($data['email']);
		$province	 = trim($data['province']);
		$city		 = trim($data['city']);
		$area		 = trim($data['area']);
		$address	 = trim($data['address']);
		$zipcode	 = trim($data['zipcode']);
		$ext_ids	 = trim($data['ext_ids']);
		$ext_values	 = trim($data['ext_values']);

		//RC4密码
		$password = self::RC4($passwd,ApiKey);
		$time = time();

		$PostData = array();
		$PostData['sbs_id']		 = sbs_id;
		$PostData['card_no']	 = $cardno;
		$PostData['password']	 = $password;
		$PostData['realname']	 = $realname;
		$PostData['gender']		 = $gender;
		$PostData['birthday']	 = $birthday;
		$PostData['idnum']		 = $idnum;
		$PostData['phone']		 = $phone;
		$PostData['mobile']		 = $mobile;
		$PostData['email']		 = $email;
		$PostData['province']	 = $province;
		$PostData['city']		 = $city;
		$PostData['area']		 = $area;
		$PostData['address']	 = $address;
		$PostData['zipcode']	 = $zipcode;
		$PostData['ext_ids']	 = $ext_ids;
		$PostData['ext_values']	 = $ext_values;
		$PostData['t']			 = $time;
		$PostData['sign']		 = md5(sbs_id.$cardno.$password.$realname.$gender.$birthday.$idnum.$phone.$mobile.$email.$province.$city.$area.$address.$zipcode.$ext_ids.$ext_values.$time.ApiKey);
		$result = self::POST_Api(ApiURL."modify_member.php?",$PostData);
		return $result;
	}

	public static function view_invoice($data){
		$cardno			 = trim($data['cardno']);
		$type			 = trim($data['type']);
		$sdate			 = trim($data['sdate']);
		$edate			 = trim($data['edate']);
		$page			 = trim($data['page']);


		//RC4密码
		// $password = RC4($passwd,ApiKey);
		$time = time();

		$PostData = array();
		$PostData['sbs_id']			 = sbs_id;
		$PostData['card_no']		 = $cardno;
		$PostData['type']			 = $type;
		$PostData['sdate']			 = $sdate;
		$PostData['edate']			 = $edate;
		$PostData['page']			 = $page;
		$PostData['t']				 = $time;
		$PostData['sign']			 = md5(sbs_id.$cardno.$type.$sdate.$edate.$page.$time.ApiKey);
		$result = self::GET_Api(ApiURL."view_invoice.php?",$PostData);
		return json_decode($result);
	}

	//积分返回规则
	public static function rule_range($type=0){
		$time = time();
		$PostData = array();
		$PostData['sbs_id']			 = sbs_id;
		$PostData['type']		 = $type;
		$PostData['t']				 = $time;
		$PostData['sign']			 = md5(sbs_id.$type.$time.ApiKey);
		$result = self::GET_Api(ApiURL.'rule_range.php?',$PostData);
		return json_decode($result);
	}

	public static function buy_card_no($data){
		$card_num		 = trim($data['card_num']);
		$order_number	 = trim($data['order_number']);

		//RC4密码
		$password = self::RC4($passwd,ApiKey);
		$time = time();
		$PostData = array();
		$PostData['sbs_id']			 = sbs_id;
		$PostData['card_num']		 = $card_num;
		$PostData['order_number']	 = $order_number;
		$PostData['t']				 = $time;
		$PostData['sign']			 = md5(sbs_id.$card_num.$order_number.$time.ApiKey);
		$result = self::POST_Api(ApiURL."buy_card_no.php?",$PostData);
		return json_decode($result);
	}

	//关注送优惠券
	public static function set_coupon($data){
		$batch_name = trim($data['batch_name']);
		$amount = trim($data['amount']);
		$status = trim($data['status']);
		$inv_type = trim($data['inv_type']);
		$user_mobile = trim($data['user_mobile']);
		$time = time();

		$PostData = array();
		$PostData['sbs_id']		 = sbs_id;
		$PostData['batch_name']	 = $batch_name;
		$PostData['amount']	 = $amount;
		$PostData['status']	 = $status;
		$PostData['inv_type']	 = $inv_type;
		$PostData['user_mobile']	 = $user_mobile;
		$PostData['t']			 = $time;
		$PostData['sign']		 = md5(sbs_id.$batch_name.$amount.$status.$inv_type.$user_mobile.$time.ApiKey);
		$result = self::POST_Api(ApiURL."set_coupon.php?",$PostData);
		return json_decode($result);
	}
	//查询会员优惠券
	public static function buy_coupon($data){
		$user_mobile = trim($data['user_mobile']);
		$status = trim($data['status']);
		$inv_type = trim($data['inv_type']);
		//RC4密码
		$time = time();

		$PostData = array();
		$PostData['sbs_id']		 = sbs_id;
		$PostData['user_mobile'] = $user_mobile;
		$PostData['status'] = $status;
		$PostData['inv_type'] = $inv_type;
		$PostData['t']	= $time;
		$PostData['sign']= md5(sbs_id.$user_mobile.$status.$inv_type.$time.ApiKey);
		$result = self::POST_Api(ApiURL."buy_coupon.php?",$PostData);
		return json_decode($result);

	}

	//发送短信
	public function send_sms($data){
		$mobile = trim($data['mobile']);
		$contents = trim($data['contents']);
		$time = time();

		$PostData = array();
		$PostData['sbs_id']		 = sbs_id;
		$PostData['mobile'] = $mobile;
		$PostData['contents'] = $contents;
		$PostData['t']	= $time;
		$PostData['sign']= md5(sbs_id.$mobile.$contents.$time.ApiKey);
		$result = self::GET_Api(ApiURL."send_sms.php?",$PostData);
		return json_decode($result);
	}
}

?>